package gui;

import java.util.Collection;

import javax.swing.table.DefaultTableModel;

import toolkit.Debug;

public class TableModel extends DefaultTableModel
{
	private String[] columns;
	
	private Object[][] rows;
	
	private Object[] rowSources;
	
	public int getColumnCount()
	{
		return columns != null ? columns.length : 0;
	}
	
	public String getColumnName(int column)
	{
		return columns[column];
	}

	public int getRowCount()
	{
		return rows != null ? rows.length : 0;
	}
	
	public Object getValueAt(int row, int column)
	{
		return rows[row][column];
	}
	
	public void setValueAt(Object value, int row, int column)
	{
		rows[row][column] = value;
		
	    fireTableCellUpdated(row, column);
	}
	
	public void setColumns(String[] columns)
	{
		this.columns = columns;
		
		fireTableStructureChanged();
	}
	
	public void setRows(Object[][] rows)
	{
		this.rows = rows;
		
		fireTableDataChanged();
	}
	
	public void setData(String[] columns, Object[][] rows)
	{
		this.columns = columns;
		
		this.rows = rows;
		
		fireTableStructureChanged();
	}
	
	public void setRowSources(Collection sources)
	{
		setRowSources(sources.toArray());
	}
	
	public void setRowSources(Object[] sources)
	{
		this.rowSources = sources;
	}
	
	public Object getRowSource(int row)
	{
		return rowSources != null ? rowSources[row] : null;
	}
	
	public Object[] getRowSources()
	{
		return rowSources;
	}
	
	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		return false;
	}
}
